import os
from building import *

cwd = GetCurrentDir()

Import('asenv')
MODULES=asenv['MODULES']
BDIR = asenv['BDIR']

objs = []

asenv.Append(CPPPATH=[cwd])

if(IsPlatformWindows() and ('LUA_PEAK_CAN' in MODULES)):
    peakcan = Package('https://www.peak-system.com/fileadmin/media/files/pcan-basic.zip')
    asenv.Append(CPPPATH=['%s/PCAN-Basic API/Include'%(peakcan)])
    if(asenv['mingw64']):
        MKSymlink('%s/"PCAN-Basic API"/x64/PCANBasic.dll'%(peakcan),
                  '%s/PCANBasic.dll'%(asenv['ASROOT']))
    else:
        MKSymlink('%s/"PCAN-Basic API"/Win32/PCANBasic.dll'%(peakcan),
                  '%s/PCANBasic.dll'%(asenv['ASROOT']))

if(IsPlatformWindows() and ('LUA_ZLG_CAN' in MODULES)):
    zlgcan_o = Package('http://www.zlg.cn/data/upload/software/Can/CAN_lib.rar')
    zlgcan = str(Glob('%s/CAN*'%(zlgcan_o))[0])
    if(asenv['mingw64']):
        asenv.Append(CPPPATH=['%s/ControlCAN/64'%(zlgcan)])
        MKSymlink('%s/ControlCAN/64/ControlCAN.dll'%(zlgcan),
                  '%s/ControlCAN.dll'%(asenv['ASROOT']))
    else:
        asenv.Append(CPPPATH=['%s/ControlCAN/32'%(zlgcan)])
        MKSymlink('%s/ControlCAN/32/ControlCAN.dll'%(zlgcan),
                  '%s/ControlCAN.dll'%(asenv['ASROOT']))

if(IsPlatformWindows() and ('LUA_VXL_CAN' in MODULES)):
    MKSymlink('%s/../../cancasexl.access/vxlapi.h'%(cwd),'%s/vxlapi.h'%(cwd))
    MKSymlink('%s/../../cancasexl.access/xlLoadlib.cpp'%(cwd),'%s/xlLoadlib.c'%(cwd))
    asenv.Append(CPPDEFINES=['DYNAMIC_XLDRIVER_DLL', 'POINTER_32='])

objs += Glob('lascanlib.c')

if('LUA_SERIAL_CAN' in MODULES):
    objs +=  Glob('serial_can.c')

if('LUA_PEAK_CAN' in MODULES):
    objs += Glob('peak_can.c')
    asenv.Append(LIBS=['PCANBasic'])

if('LUA_ZLG_CAN' in MODULES):
    objs += Glob('zlg_can.c')
    asenv.Append(LIBS=['ControlCAN'])

if('LUA_VXL_CAN' in MODULES):
    objs += Glob('vxl_can.c')
    objs += Glob('xlLoadlib.c')

if('LUA_SOCKET_CAN' in MODULES):
    if(IsPlatformWindows()):
        asenv.Append(LIBS=['wsock32'])
    objs += Glob('socket_can.c')
    objs += Glob('socketwin_can.c')

if(IsPlatformWindows()):
    if(asenv['mingw64']):
        if('LUA_PEAK_CAN' in MODULES):
            MKSymlink('%s/"PCAN-Basic API"/x64/VC_LIB/PCANBasic.lib'%(peakcan), '%s/libPCANBasic.a'%(BDIR))
        if('LUA_ZLG_CAN' in MODULES):
            MKSymlink('%s/ControlCAN/64/ControlCAN.lib'%(zlgcan), '%s/libControlCAN.a'%(BDIR))
    else:
        if('LUA_PEAK_CAN' in MODULES):
            MKSymlink('%s/"PCAN-Basic API"/Win32/VC_LIB/PCANBasic.lib'%(peakcan), '%s/libPCANBasic.a'%(BDIR))
        if('LUA_ZLG_CAN' in MODULES):
            MKSymlink('%s/ControlCAN/32/ControlCAN.lib'%(zlgcan), '%s/libControlCAN.a'%(BDIR))
    asenv.Append(LIBPATH=[BDIR])

src = '%s/socket_can_driver.c'%(cwd)
tgt = '%s/build/%s/socket_can_driver.exe'%(asenv['ASROOT'], os.name)
cmd = 'gcc %s -I%s/com/as.infrastructure/include -o %s'%(src,asenv['ASROOT'],tgt)
if(IsPlatformWindows()):
    cmd += ' -D__WINDOWS__ -lwsock32'
else:
    cmd += ' -D__LINUX__'
MKObject([src], tgt, cmd)

Return('objs')
